package com.kjlink.internal.workreport.jira.model;


import cn.xuqiudong.common.base.model.BaseEntity;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.kjlink.internal.workreport.base.annotation.NoneColumn;
import com.kjlink.internal.workreport.base.constant.CommonConstant;
import com.kjlink.internal.workreport.base.enums.JiraBugType;
import com.kjlink.internal.workreport.base.enums.JiraCheckResult;
import com.kjlink.internal.workreport.helper.ReportCommonHelper;
import com.kjlink.internal.workreport.helper.SessionHelper;
import com.kjlink.internal.workreport.user.model.User;
import com.kjlink.internal.workreport.util.NumberUtils;
import org.springframework.data.relational.core.sql.In;
import org.springframework.format.annotation.DateTimeFormat;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;


/**
 * jira信息 实体类
 *
 * @author Vic.xu
 */
public class Jira extends BaseEntity {

    private static final long serialVersionUID = 1L;

    /**
     * 所属项目
     */
    private Integer projectId;

    @NoneColumn
    private String projectName;

    /**
     * jira号
     */
    private String code;

    /**
     * 父jira号
     */
    private String pcode;

    /**
     * 经办人
     */
    private String assignee;

    /**
     * 报告人
     */
    private String reporter;

    /**
     * 内容
     */
    private String content;

    /**
     * 预计工时：分
     */
    private Integer duration;

    /**
     * 已用工时
     */
    private Integer usedTime;

    /**
     * 子任务已用工时
     */
    private Integer subUsedTime;

    /**
     * 总工时： 已用工时 + 子任务已用工时
     */
    private Integer totalUsedTime;

    /**
     * 检测结果:对jira系统
     */
    private JiraCheckResult checkResult;

    /**
     * 状态:open,closed...
     */
    private String status;

    /**
     * 类型:bug,task,improve
     */
    private String type;

    /**
     * 结算年度
     */
    private Integer settleYear;

    /**
     * 修复日期
     */
    @JsonFormat(pattern = "yyyy-MM-dd", timezone = "GMT+8")
    @DateTimeFormat(pattern = "yyyy-MM-dd")
    private Date releaseDate;

    /**
     * 原始jira的创建时间
     */
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm", timezone = "GMT+8")
    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm")
    private Date creadted;

    /**
     * 版本
     */
    private String versionName;

    /**
     * 计划开始时间
     */
    @JsonFormat(pattern = "yyyy-MM-dd", timezone = "GMT+8")
    @DateTimeFormat(pattern = "yyyy-MM-dd")
    private Date planStartDate;

    /**
     * 计划完成时间
     */
    @JsonFormat(pattern = "yyyy-MM-dd", timezone = "GMT+8")
    @DateTimeFormat(pattern = "yyyy-MM-dd")
    private Date planEndDate;

    /**
     * 实际完成时间
     */
    @JsonFormat(pattern = "yyyy-MM-dd", timezone = "GMT+8")
    @DateTimeFormat(pattern = "yyyy-MM-dd")
    private Date actualCompleteDate;

    /**
     * bug类型，内测bug、UATbug、生产bug
     */
    private JiraBugType bugType;

    @NoneColumn
    private String createName;

    /**
     * 预计工时 小时
     */
    @NoneColumn
    // 这里指定序列化和反序列化时的格式，比如保留两位小数
    @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "0.00")
    private BigDecimal durationHour;

    /**
     * 已使用工时：小时
     */
    @NoneColumn
    // 这里指定序列化和反序列化时的格式，比如保留两位小数
    @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "0.00")
    private BigDecimal usedTimeHour;

    /**
     * 总用工时：小时
     */
    @NoneColumn
    // 这里指定序列化和反序列化时的格式，比如保留两位小数
    @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "0.00")
    private BigDecimal totalUsedTimeHour;

    /**
     * 子任务已用工时：小时
     */
    @NoneColumn
    @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "0.00")
    private BigDecimal subUsedTimeHour;


    /**
     * 多选 状态:open,closed...
     */
    @NoneColumn
    @JsonProperty(access = JsonProperty.Access.WRITE_ONLY)
    private List<String> statusList;

    /**
     * 多选类型:bug,task,improve
     */
    @NoneColumn
    @JsonProperty(access = JsonProperty.Access.WRITE_ONLY)
    private List<String> typeList;

    /**
     * 经办人小组
     */
    @NoneColumn
    @JsonProperty(access = JsonProperty.Access.WRITE_ONLY)
    private Integer assigneeGroup;

    /**
     * 查询工时是否超时: 已用工时大于预计工时
     */
    @NoneColumn
    @JsonProperty(access = JsonProperty.Access.WRITE_ONLY)
    private Boolean overflow;


    /***************** set|get  start **************************************/

    public BigDecimal getDurationHour() {
        if (duration != null && durationHour == null) {
            durationHour = ReportCommonHelper.toHoursNumber(duration);
        }
        return durationHour;
    }

    public void setDurationHour(BigDecimal durationHour) {
        if (durationHour != null) {
            duration = ReportCommonHelper.toMinutesNumber(durationHour).intValue();
        }
        this.durationHour = durationHour;
    }

    public BigDecimal getUsedTimeHour() {
        if (usedTime != null && usedTimeHour == null) {
            usedTimeHour = ReportCommonHelper.toHoursNumber(usedTime);
        }
        return usedTimeHour;
    }

    public void setUsedTimeHour(BigDecimal usedTimeHour) {
        if (usedTimeHour != null) {
            usedTime = ReportCommonHelper.toMinutesNumber(usedTimeHour).intValue();
        }
        this.usedTimeHour = usedTimeHour;
    }

    /**
     * set：所属项目
     */
    public Jira setProjectId(Integer projectId) {
        this.projectId = projectId;
        return this;
    }

    /**
     * get：所属项目
     */
    public Integer getProjectId() {
        return projectId;
    }

    /**
     * set：jira号
     */
    public Jira setCode(String code) {
        this.code = code;
        return this;
    }

    /**
     * get：jira号
     */
    public String getCode() {
        return code;
    }

    /**
     * set：父jira号
     */
    public Jira setPcode(String pcode) {
        this.pcode = pcode;
        return this;
    }

    /**
     * get：父jira号
     */
    public String getPcode() {
        return pcode;
    }

    /**
     * set：经办人
     */
    public Jira setAssignee(String assignee) {
        this.assignee = assignee;
        return this;
    }

    /**
     * get：经办人
     */
    public String getAssignee() {
        return assignee;
    }

    /**
     * set：报告人
     */
    public Jira setReporter(String reporter) {
        this.reporter = reporter;
        return this;
    }

    /**
     * get：报告人
     */
    public String getReporter() {
        return reporter;
    }

    /**
     * set：内容
     */
    public Jira setContent(String content) {
        this.content = content;
        return this;
    }

    /**
     * get：内容
     */
    public String getContent() {
        return content;
    }

    /**
     * set：预计工时：分
     */
    public Jira setDuration(Integer duration) {
        this.duration = duration;
        return this;
    }

    /**
     * get：预计工时：分
     */
    public Integer getDuration() {
        return duration;
    }

    /**
     * set：已用工时
     */
    public Jira setUsedTime(Integer usedTime) {
        this.usedTime = usedTime;
        return this;
    }

    /**
     * get：已用工时
     */
    public Integer getUsedTime() {
        return usedTime;
    }

    /**
     * set：子任务已用工时
     */
    public Jira setSubUsedTime(Integer subUsedTime) {
        this.subUsedTime = subUsedTime;
        return this;
    }

    /**
     * get：子任务已用工时
     */
    public Integer getSubUsedTime() {
        return subUsedTime;
    }

    /**
     * set：检测结果:对jira系统
     */
    public Jira setCheckResult(JiraCheckResult checkResult) {
        this.checkResult = checkResult;
        return this;
    }

    /**
     * get：检测结果:对jira系统
     */
    public JiraCheckResult getCheckResult() {
        return checkResult;
    }

    /**
     * set：状态:open,closed...
     */
    public Jira setStatus(String status) {
        this.status = status;
        return this;
    }

    /**
     * get：状态:open,closed...
     */
    public String getStatus() {
        return status;
    }

    /**
     * set：类型:bug,task,improve
     */
    public Jira setType(String type) {
        this.type = type;
        return this;
    }

    /**
     * get：类型:bug,task,improve
     */
    public String getType() {
        return type;
    }

    /**
     * set：结算年度
     */
    public Jira setSettleYear(Integer settleYear) {
        this.settleYear = settleYear;
        return this;
    }

    /**
     * get：结算年度
     */
    public Integer getSettleYear() {
        return settleYear;
    }

    /**
     * set：修复日期
     */
    public Jira setReleaseDate(Date releaseDate) {
        this.releaseDate = releaseDate;
        return this;
    }

    /**
     * get：修复日期
     */
    public Date getReleaseDate() {
        return releaseDate;
    }

    /**
     * set：版本
     */
    public Jira setVersionName(String versionName) {
        this.versionName = versionName;
        return this;
    }

    /**
     * get：版本
     */
    public String getVersionName() {
        return versionName;
    }

    public String getProjectName() {
        return projectName;
    }

    public void setProjectName(String projectName) {
        this.projectName = projectName;
    }

    public String getCreateName() {
        return createName;
    }

    public void setCreateName(String createName) {
        this.createName = createName;
    }

    public List<String> getTypeList() {
        return typeList;
    }

    public void setTypeList(List<String> typeList) {
        this.typeList = typeList;
    }

    public List<String> getStatusList() {
        return statusList;
    }

    public void setStatusList(List<String> statusList) {
        this.statusList = statusList;
    }

    public Integer getAssigneeGroup() {
        return assigneeGroup;
    }

    public void setAssigneeGroup(Integer assigneeGroup) {
        this.assigneeGroup = assigneeGroup;
    }

    public Date getCreadted() {
        return creadted;
    }

    public void setCreadted(Date creadted) {
        this.creadted = creadted;
    }

    public Integer getTotalUsedTime() {
        return totalUsedTime;
    }

    public void setTotalUsedTime(Integer totalUsedTime) {
        this.totalUsedTime = totalUsedTime;
    }

    public Date getPlanStartDate() {
        return planStartDate;
    }

    public void setPlanStartDate(Date planStartDate) {
        this.planStartDate = planStartDate;
    }

    public Date getPlanEndDate() {
        return planEndDate;
    }

    public void setPlanEndDate(Date planEndDate) {
        this.planEndDate = planEndDate;
    }

    public Date getActualCompleteDate() {
        return actualCompleteDate;
    }

    public void setActualCompleteDate(Date actualCompleteDate) {
        this.actualCompleteDate = actualCompleteDate;
    }

    public JiraBugType getBugType() {
        return bugType;
    }

    public void setBugType(JiraBugType bugType) {
        this.bugType = bugType;
    }

    public String getBugTypeText() {
        if (bugType == null) {
            return "";
        }
        return bugType.getText();
    }

    /***************** set|get  end **************************************/


    /**
     * 是否可编辑用户
     *
     * @return boolean
     */
    public boolean getCouldEdit() {
        User user = SessionHelper.currentUser();
        if (user == null) {
            return false;
        }
        if (Boolean.TRUE.equals(user.getDevManager())) {
            return true;
        }
        if (NumberUtils.equals(this.id, user.getId())) {
            return true;
        }
        return CommonConstant.SPECIAL_USER_CODE.equals(user.getUserCode());
    }


    /**
     * jira 总工时
     */
    public String getTotalPersonDays() {
        return ReportCommonHelper.toPersonDays(this.duration);
    }

    public BigDecimal getTotalUsedTimeHour() {
        if (totalUsedTimeHour == null) {
            Integer total = NumberUtils.add(this.usedTime, this.subUsedTime);
            totalUsedTimeHour = ReportCommonHelper.toHoursNumber(total);
        }
        return totalUsedTimeHour;
    }

    public void setTotalUsedTimeHour(BigDecimal totalUsedTimeHour) {
        this.totalUsedTimeHour = totalUsedTimeHour;
    }

    public BigDecimal getSubUsedTimeHour() {
        if (subUsedTimeHour == null) {
            subUsedTimeHour = ReportCommonHelper.toHoursNumber(subUsedTime);
        }
        return subUsedTimeHour;
    }

    public void setSubUsedTimeHour(BigDecimal subUsedTimeHour) {
        this.subUsedTimeHour = subUsedTimeHour;
    }

    /**
     * 已经使用的总工时
     */
    public String getTotalUsedPersonDays() {
        if (usedTime == null) {
            return ReportCommonHelper.toPersonDays(this.subUsedTime);
        }
        if (subUsedTime == null) {
            return ReportCommonHelper.toPersonDays(this.usedTime);
        }
        return ReportCommonHelper.toPersonDays(this.usedTime + this.subUsedTime);
    }


    public Boolean getOverflow() {
        return overflow;
    }

    public void setOverflow(Boolean overflow) {
        this.overflow = overflow;
    }

    /**
     * 自身使用的总工时
     *
     * @return
     */
    public String getUsedPersonDays() {
        return ReportCommonHelper.toPersonDays(this.usedTime);
    }

    /**
     * 子jira使用的工时
     */
    public String getSubUsedPersonDays() {
        return ReportCommonHelper.toPersonDays(this.subUsedTime);
    }
}
